home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gigarom 1
/
Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso
/
FILES
/
DEV
/
A-B
/
AETracker.sea
/
AETracker Monitor.c
next >
Wrap
C/C++ Source or Header
|
1992-08-11
|
24KB
|
669 lines
/* AETracker Monitor */
/* 8/11/92 */
/* A sample program demonstrating the programatic interface to
version 3.0 and later of the AETracker INIT/Control Panel
*/
/*
Application based on sample code provided by Apple Developer Technical Support.
AETracker interface routines provided by RavenWare Software
*/
#include <Types.h>
#include <memory.h>
#include <Packages.h>
#include <Errors.h>
#include <quickdraw.h>
#include <fonts.h>
#include <dialogs.h>
#include <windows.h>
#include <menus.h>
#include <events.h>
#include <OSEvents.h>
#include <Desk.h>
#include <diskinit.h>
#include <OSUtils.h>
#include <resources.h>
#include <toolutils.h>
#include <AppleEvents.h>
#include <EPPC.h>
#include <GestaltEqu.h>
#include <PPCToolbox.h>
#include <Processes.h>
#include <Balloons.h>
#include "AETracker.h"
/* prototypes */
void InitalizeApp(void);
void DoDiskEvents(long dinfo); /* hi word is error code, lo word is drive number */
void DrawMain(WindowPtr drawIt);
Boolean DoSelected(long val);
void SpitStatus(AETrackExtParamPtr AETStatus);
void InitAEStuff(void);
void DoHighLevel(EventRecord *AERecord);
void DoDaCall(MenuHandle themenu, long theit);
pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
void SampleHelpDialog(void);
/* one external */
extern void _DataInit(); /* this is the C initialization code */
#define kMBarID 128
#define kAppleMenu 128
#define kFileMenu 129
#define kEditMenu 130
#define kMonitorMenu 131
#define kResumeMask 1 /* bit of message field for resume vs. suspend */
#define kSampHelp 129
#define kAboutBox 128
#define kHelpString 128
#define kNewItem 1
#define kOpenItem 2
#define kCloseItem 3
#define kSaveItem 4
#define kSaveAsItem 5
#define kFileBlank1 6
#define kPageSetupItem 7
#define kPrintItem 8
#define kFileBlank2 9
#define kQuitItem 10
#define kBadSystem 130
#define kTitle 129
#define kStatStrings 128
enum {
kAETActiveWord = 3, kOutFile = 5, kOnWord = 8, kOffWord, kNonEx, kEx, kRefCon, kRAddress
};
TEHandle theTE;
struct AEinstalls {
AEEventClass theClass;
AEEventID theEvent;
EventHandlerProcPtr theProc;
};
typedef struct AEinstalls AEinstalls;
Boolean gAETInstalled;
AETrackExtParam myParams;
AETrackerStatusBlock myAETStatus;
Handle gMymenu; /* my menu bar handle */
MenuHandle gAppleMenuHandle, gFileMenuHandle, gEditMenuHandle, gMonitorMenuHandle;
Boolean gQuit, gInBackground;
EventRecord gERecord;
AEDesc gTheAddress;
WindowPtr myWindow = nil;
ProcessSerialNumber gOurSN;
short gHelpItem;
AETrackExternProcPtr externAE;
pascal OSErr SampleIntercept(ParmBlkPtr paramBlock, long refCon);
pascal OSErr SampleParseIntercept(Ptr AEParmPtr, long AESelector, long refCon);
#pragma segment Main
main()
{
OSErr myErr;
WindowPtr twindow;
UnloadSeg((Ptr)_DataInit); /* throw out setup code */
InitalizeApp();
UnloadSeg((Ptr)InitalizeApp); /* get rid of my initialization code */
myErr = Gestalt('CKI3', (long *)&externAE);
if (myErr != noErr) {
short qq;
gAETInstalled = false;
StopAlert(131, nil);
for (qq = 1; qq < 4; qq++) {
DisableItem(GetMHandle(kMonitorMenu), qq);
}
} else {
gAETInstalled = true;
}
do {
WaitNextEvent(everyEvent, &gERecord, 30, nil);
switch (gERecord.what) {
case nullEvent:
/* no nul processing in this sample */
if (!gInBackground && myWindow)
TEIdle(theTE);
break;
case updateEvt:
DrawMain((WindowPtr)gERecord.message); /* draw whatever window needs an update */
break;
case mouseDown:
/* first see where the hit was */
switch (FindWindow(gERecord.where, &twindow)) {
case inDesk: /* if they hit in desk, then the process manager */
break; /* will switch us out, we don't need to do anything */
case inMenuBar:
DoSelected(MenuSelect(gERecord.where));
break;
case inSysWindow:
/* pass to the system */
SystemClick(&gERecord, twindow);
break;
case inContent:
/* Handle content and control clicks here */
GlobalToLocal(&gERecord.where);
TEClick(gERecord.where, false, theTE);
break;
case inDrag:
if (twindow == FrontWindow())
DragWindow(twindow, gERecord.where, &qd.screenBits.bounds);
break;
case inGrow:
/* Call GrowWindow here if you have a grow box */
break;
case inGoAway:
/* Click in Close box */
break;
}
case mouseUp:
/* don't care */
break;
/* same action for key or auto key */
case keyDown:
case autoKey:
if (gERecord.modifiers & cmdKey)
DoSelected(MenuKey(gERecord.message & charCodeMask));
else
TEKey(gERecord.message & charCodeMask, theTE);
break;
case keyUp:
/* don't care */
break;
case diskEvt:
/* I don't do anything special for disk events, this just passes them */
/* to a function that checks for an error on the mount */
DoDiskEvents(gERecord.message);
break;
case activateEvt:
if (gERecord.modifiers & activeFlag)
DrawMain((WindowPtr)gERecord.message);
break;
case networkEvt:
/* don't care */
break;
case driverEvt:
/* don't care */
break;
case app4Evt:
switch ((gERecord.message >> 24) & 0x0FF) { /* high byte of message */
case suspendResumeMessage: /* suspend/resume is also an activate/deactivate */
gInBackground = (gERecord.message & kResumeMask) == 0;
if (gInBackground)
TEDeactivate(theTE);
else
TEActivate(theTE);
break;
}
break;
default:
break;
/* This dispatches high level events (AppleEvents, for example) */
/* to our dispatch routine. This is NEW in the event loop for */
/* System 7 */
case kHighLevelEvent:
DoHighLevel(&gERecord);
break;
}
}
while (gQuit != true);
}
/* DoDaCall opens the requested DA. It's here as a seperate routine if you'd */
/* like to perform some action or just know when a DA is opened in your */
/* layer. Can be handy to track memory problems when a DA is opened */
/* with an Option-open */
void DoDaCall(MenuHandle themenu, long theit)
{
long qq;
char DAname[255];
GetItem(themenu, theit, &DAname);
qq = OpenDeskAcc(DAname);
}
/* end DoDaCall */
/* DoDiskEvents just checks the error code from the disk mount, */
/* and puts up the 'Format' dialog (through DIBadMount) if need be */
/* You can do much more here if you care about what disks are */
/* in the drive */
void DoDiskEvents(long dinfo) /* hi word is error code, lo word is drive number */
{
short hival, loval, tommy;
Point fredpoint = {
40, 40
};
hival = HiWord(dinfo);
loval = LoWord(dinfo);
if (hival != noErr) /* something happened */ {
tommy = DIBadMount(fredpoint, dinfo);
}
}
/* draws my window. Pretty simple */
void DrawMain(WindowPtr drawIt)
{
BeginUpdate(drawIt);
SetPort(drawIt);
EraseRect(&drawIt->portRect);
TEUpdate(&drawIt->portRect, theTE);
EndUpdate(drawIt);
}
/* my menu action taker. It returns a Boolean which I usually ignore, but it */
/* mught be handy someday */
Boolean DoSelected(long val)
{
OSErr myErr = noErr;
short loval, hival;
Boolean returnVal = false;
loval = LoWord(val);
hival = HiWord(val);
switch (hival) { /* switch off the menu number selected */
case kAppleMenu: /* Apple menu */
if (loval != 1) { /* if this was not About, it's a DA */
DoDaCall(gAppleMenuHandle, loval);
} else {
Alert(kAboutBox, nil); /* do about box */
}
returnVal = true;
break;
case kFileMenu: /* File menu */
switch (loval) {
case kQuitItem:
gQuit = true; /* only item */
returnVal = true;
break;
default:
break;
}
break;
case kEditMenu:
/* edit menu junk */
/* don't care */
break;
case kMonitorMenu:
/* add all your test stuff here */
/* only one item, kill stuff */
switch (loval) {
StandardFileReply myReply;
case 1:
StandardPutFile("\p put it", "\pmytrack", &myReply);
if (myReply.sfGood) {
myParams.outFile = myReply.sfFile;
myParams.flags = kAETUsePassedFileFSSpec;
}
myErr = externAE(&myParams, kAETToggle); /* Toggle */
break;
case 2:
myParams.dataPtrs.statusData = &myAETStatus;
myErr = externAE(&myParams, kAETStatus); /* Status */
SpitStatus(&myParams);
break;
case 3:
myParams.procs.outputPtr = SampleIntercept;
/* pass my A5 so it will come back to me later */
myParams.refCon = SetCurrentA5();
myErr = externAE(&myParams, kAETInterceptOutput); /* Intercept output */
break;
case 4:
myErr = externAE(&myParams, kAETReconnectOutput); /* reconnect output */
break;
case 5:
myParams.procs.outputParsePtr = SampleParseIntercept;
/* pass my A5 so it will come back to me later */
myParams.refCon = SetCurrentA5();
myErr = externAE(&myParams, kAETInterceptParse); /* intercept routines */
break;
case 6:
myErr = externAE(&myParams, kAETReconnectParse); /* reconnect routines */
break;
case 7:
myErr = externAE(nil,kAETEmergencyReset);
break;
}
if(myErr)Debugger();
break;
case kHMHelpMenuID: /* Defined in Balloons.h */
/* I only care about this item. If anything else is returned here, I don't know what */
/* it is, so I leave it alone. Remember, the Help Manager chapter says that */
/* Apple reserves the right to add and change things in the Help menu */
if (loval == gHelpItem)
SampleHelpDialog();
break;
}
HiliteMenu(0);
return(returnVal);
}
/* InitAEStuff installs my appleevent handlers */
void InitAEStuff(void)
{
static AEinstalls HandlersToInstall[] = { {
kCoreEventClass, kAEOpenApplication, AEOpenHandler
}, {
kCoreEventClass, kAEOpenDocuments, AEOpenDocHandler
}, {
kCoreEventClass, kAEQuitApplication, AEQuitHandler
}, {
kCoreEventClass, kAEPrintDocuments, AEPrintHandler
},
/* The above are the four required AppleEvents. */
};
OSErr aevtErr = noErr;
long aLong = 0;
Boolean gHasAppleEvents = false;
/* Check this machine for AppleEvents. If they are not here (ie not 7.0)
* then we exit */
gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
/* The following series of calls installs all our AppleEvent Handlers.
* These handlers are added to the application event handler list that
* the AppleEvent manager maintains. So, whenever an AppleEvent happens
* and we call AEProcessEvent, the AppleEvent manager will check our
* list of handlers and dispatch to it if there is one.
*/
if (gHasAppleEvents) {
register qq;
for (qq = 0; qq < ((sizeof(HandlersToInstall) / sizeof(AEinstalls))); qq++) {
aevtErr = AEInstallEventHandler(HandlersToInstall[qq].theClass, HandlersToInstall[qq].theEvent,
HandlersToInstall[qq].theProc, 0, false);
if (aevtErr) {
ExitToShell(); /* just fail, baby */
}
}
} else {
ExitToShell();
}
}
/* end InitAEStuff */
/* I'm not doing error handling in this sample for clarities sake, you should. Hah, */
/* easy for me to say, huh? */
void DoHighLevel(EventRecord *AERecord)
{
AEProcessAppleEvent(AERecord);
}
/* end DoHighLevel */
/* This is the standard Open Application event. */
pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
{
Rect theR;
#pragma unused (messagein,reply,refIn)
/* we of course don't do anything here in this simple app */
/* except open our window */
myWindow = GetNewWindow(128, nil, (WindowPtr)-1);
SetPort(myWindow);
theTE = TENew(&myWindow->portRect, &myWindow->portRect);
TEActivate(theTE);
TEAutoView(true, theTE);
return(noErr);
}
/* end AEOpenHandler */
/* Open Doc, opens our documents. Remember, this can happen at application start AND */
/* anytime else. If your app is up and running and the user goes to the desktop, hilites one */
/* of your files, and double-clicks or selects Open from the finder File menu this event */
/* handler will get called. Which means you don't do any initialization of globals here, or */
/* anything else except open the doc. */
/* SO-- Do NOT assume that you are at app start time in this */
/* routine, or bad things will surely happen to you. */
pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
{
#pragma unused (messagein,refIn,reply)
/* we of course don't do anything here */
return(errAEEventNotHandled); /* we have no docs, so no odoc events should come to us */
}
pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
{ /* no printing handler in yet, so we'll ignore this */
/* the operation is functionally identical to the ODOC event, with the additon */
/* of calling your print routine. */
#pragma unused (messagein,refIn,reply)
/* we of course don't do anything here */
return(errAEEventNotHandled); /* we have no docs, so no pdoc events should come to us */
}
/* Standard Quit event handler, to handle a Quit event from the Finder, for example. */
/* ••••• DO NOT CALL EXITTOSHELL HERE ••••• or you will never have a happy life. */
pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
{
#pragma unused (messagein,refIn,reply)
/* prepQuit sets the Stop flag for us. It does _NOT_ quit, you */
/* should NEVER quit from an AppleEvent handler. Calling */
/* ExitToShell here would blow things up */
gQuit = true;
return(noErr);
}
/* This is my sample help dialog. Does not do anything, expand as you need */
void SampleHelpDialog(void)
{
DialogPtr tdial = GetNewDialog(kSampHelp, nil, (WindowPtr)-1);
short itemhit = 0;
while (itemhit != 1) {
ModalDialog((ModalFilterProcPtr)nil, &itemhit);
}
DisposDialog(tdial);
}
/* This is a sample Text intercept routine */
pascal OSErr SampleIntercept(ParmBlkPtr paramBlock, long refCon)
{
OSErr myErr = noErr;
long oldA5;
Rect sampRect;
WindowPtr tempWP;
GetPort(&tempWP);
/* •••• REMEMBER! A5 is set to the A5 of the application that made */
/* the AppleEvent call!. You must set it to your own A5 if you */
/* want to access anything in your application */
oldA5 = SetCurrentA5(); /* save current A5 */
/* I passed my A5 as the refCon value when I installed my intercept routine */
SetA5(refCon);
sampRect = myWindow->portRect;
SetPort(myWindow);
/* stuff the text in the TE record */
/* The parameter block that was passed to me is the same as the */
/* one used for PBWrite, so I'll use the buffer pointer and length */
TEInsert(paramBlock->ioParam.ioBuffer, paramBlock->ioParam.ioReqCount, theTE);
SetA5(oldA5); /* restore old A5 */
SetPort(tempWP); /* restore port */
return(myErr); /* no errors */
}
/* This is a sample parsing intercept routine */
pascal OSErr SampleParseIntercept(Ptr AEParmPtr, long AESelector, long refCon)
{
OSErr myErr = noErr;
OSType createdType;
/* •••• REMEMBER! A5 is set to the A5 of the application that made */
/* the AppleEvent call!. You must set it to your own A5 if you */
/* want to access anything in your application */
long oldA5 = SetCurrentA5(); /* save current A5 */
/* I passed my A5 as the refCon value when I installed my intercept routine */
SetA5(refCon);
if(AESelector == 0x0B14){ /* is AECreateAppleEvent being called? */
DebugStr("\pAECreateAppleEvent called");
/* since the AEParmPtr points to the bottom of the parameter stack, */
/* I back up 18 bytes to find the class of AppleEvent being created */
createdType = *((OSType *) (AEParmPtr + 18));
}
SetA5(oldA5);
return(myErr);
}
/* quick and dirty routine to output status information to the current window */
void SpitStatus(AETrackExtParamPtr AETStatus)
{
Str255 theString;
char CR = 0xd;
char SP = 0x20;
short qq = 1;
if (!gAETInstalled)
qq++;
GetIndString(theString, kStatStrings, qq);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
qq = kAETActiveWord;
if (!AETStatus->dataPtrs.statusData->AETActive)
qq++;
GetIndString(theString, kStatStrings, qq);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
qq = kOutFile;
GetIndString(theString, kStatStrings, qq);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&SP, 1, theTE);
TEInsert((Ptr)&AETStatus->outFile.name[1], AETStatus->outFile.name[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
qq++;
GetIndString(theString, kStatStrings, qq);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
qq = kOnWord;
if (!AETStatus->dataPtrs.statusData->fileInterceptActive)
qq++;
GetIndString(theString, kStatStrings, qq);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
qq = kNonEx;
if (AETStatus->dataPtrs.statusData->fileExclusiveIntercept)
qq++;
GetIndString(theString, kStatStrings, qq);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
qq = kRAddress;
GetIndString(theString, kStatStrings, qq);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&SP, 1, theTE);
NumToString((long)AETStatus->dataPtrs.statusData->fileInterceptRoutine, theString);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
qq = kRefCon;
GetIndString(theString, kStatStrings, qq);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&SP, 1, theTE);
NumToString(AETStatus->dataPtrs.statusData->fileRefCon, theString);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
GetIndString(theString, kStatStrings, 7);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
qq = kOnWord;
if (!AETStatus->dataPtrs.statusData->parseInterceptActive)
qq++;
GetIndString(theString, kStatStrings, qq);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
qq = kNonEx;
if (AETStatus->dataPtrs.statusData->parseExclusiveIntercept)
qq++;
GetIndString(theString, kStatStrings, qq);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
qq = kRAddress;
GetIndString(theString, kStatStrings, qq);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&SP, 1, theTE);
NumToString((long)AETStatus->dataPtrs.statusData->parseInterceptRoutine, theString);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
qq = kRefCon;
GetIndString(theString, kStatStrings, qq);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&SP, 1, theTE);
NumToString(AETStatus->dataPtrs.statusData->parseRefCon, theString);
TEInsert((Ptr)&theString[1], theString[0], theTE);
TEInsert((Ptr)&CR, 1, theTE);
}
#pragma segment Initialize
void InitalizeApp(void)
{
MenuHandle helpHandle;
StringHandle helpString;
short count;
long vers;
MaxApplZone();
InitGraf((Ptr)&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(nil);
InitCursor();
/* Check system version */
Gestalt(gestaltSystemVersion, &vers);
vers = (vers >> 8) & 0xf; /* shift result over and mask out major version number */
if (vers < 7) {
StopAlert(kBadSystem, nil);
ExitToShell();
}
InitAEStuff();
/* set up my menu junk */
gMymenu = GetNewMBar(kMBarID);
SetMenuBar(gMymenu);
gAppleMenuHandle = GetMHandle(kAppleMenu);
gFileMenuHandle = GetMHandle(kFileMenu);
gEditMenuHandle = GetMHandle(kEditMenu);
gMonitorMenuHandle = GetMHandle(kMonitorMenu);
AddResMenu(gAppleMenuHandle, 'DRVR');
/* now install my Help menu item in the Help Manager's menu */
HMGetHelpMenuHandle(&helpHandle); /* Get the Hlpe menu handle */
count = CountMItems(helpHandle); /* How many items are there? */
helpString = GetString(kHelpString); /* get my help string */
DetachResource(helpString); /* detach it */
HNoPurge(helpString);
MoveHHi((Handle)helpString);
HLock((Handle)helpString);
InsMenuItem(helpHandle, (Ptr)*helpString, count + 1); /* insert my item in the Help menu */
gHelpItem = CountMItems(helpHandle); /* The number of the item */
DrawMenuBar();
GetCurrentProcess(&gOurSN); /* Get our process serial number for later use, if needed */
}